package dataselect.radiotube;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JTextField;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;

import java.awt.Color;
import java.awt.Dimension;

public class RadiotManage extends JFrame {

	private JPanel contentPane;
	private static JTextField textField_0;//电磁阀数量
	private static JTextField textField_1;//轮灌组数量
	private static ArrayList<String> idList = new ArrayList<String>();
	private static ArrayList<String> groupList = new ArrayList<String>();
	private static ArrayList<String> radioAreaList = new ArrayList<String>();
	private static ArrayList<String> groupAreaList = new ArrayList<String>();
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		RadiotManage frame = new RadiotManage();
		frame.setVisible(true);
	}

	/**
	 * Create the frame.
	 */
	public RadiotManage() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 318);
		contentPane = new JPanel();
		setTitle("当前控制区：");
		setLocationRelativeTo(null);
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("电磁阀分组设置");
		label.setFont(new Font("华文宋体", Font.BOLD, 16));
		label.setBounds(161, 10, 119, 30);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("请输入以下信息：");
		label_1.setFont(new Font("华文宋体", Font.PLAIN, 15));
		label_1.setBounds(10, 50, 128, 15);
		contentPane.add(label_1);
		
		JLabel lblNewLabel = new JLabel("电磁阀数量：");
		lblNewLabel.setFont(new Font("华文宋体", Font.PLAIN, 15));
		lblNewLabel.setBounds(105, 95, 90, 20);
		contentPane.add(lblNewLabel);
		
		textField_0 = new JTextField();
		textField_0.setBounds(228, 91, 152, 30);
		contentPane.add(textField_0);
		textField_0.setColumns(10);
		
		JLabel label_2 = new JLabel("轮灌组数量：");
		label_2.setFont(new Font("华文宋体", Font.PLAIN, 15));
		label_2.setBounds(105, 149, 103, 15);
		contentPane.add(label_2);
		
		textField_1 = new JTextField();
		textField_1.setBounds(228, 142, 152, 30);
		contentPane.add(textField_1);
		textField_1.setColumns(10);
		
		JButton button = new JButton("确定");
		button.setForeground(Color.BLUE);
		button.setBackground(Color.WHITE);
		button.setFont(new Font("华文宋体", Font.PLAIN, 15));
		button.setBounds(228, 227, 65, 30);
		contentPane.add(button);
		button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				groupAndRadioTube();
			}
		});
		
		JButton resetButton = new JButton("重置");
		resetButton.setForeground(Color.BLUE);
		resetButton.setBackground(Color.WHITE);
		resetButton.setFont(new Font("华文宋体", Font.PLAIN, 15));
		resetButton.setBounds(315, 227, 65, 30);
		contentPane.add(resetButton);
		resetButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				textField_0.setText(" ");
				textField_1.setText(" ");
			}
		});
		
		
		
	}
	
	//分组编号和传感器编号映射
		public static void groupAndRadioTube () {
			String[][] rowData = new String[200][4];
			String[] columnData = {"轮灌组编号(共"+textField_1.getText()+"组)",
								   "电磁阀编号(共"+textField_0.getText()+"个)","电磁阀控制面积（亩）",
										   "轮灌组控制面积（亩）"};
			
			JFrame frame1 = new JFrame("电磁阀分组设置");
			frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			JPanel tJPanel = new JPanel();
			JPanel bJPanel = new JPanel();
			tJPanel.setLayout(new BorderLayout());
			bJPanel.setLayout(new BorderLayout());
			
			DefaultTableModel model = new DefaultTableModel();
			model.setDataVector(rowData, columnData);
			
			JTable table = new JTable(model);
			table.setRowHeight(25);
			table.setFont(new Font("宋体", Font.PLAIN, 15));
			table.getTableHeader().setFont(new Font("华文宋体", Font.PLAIN, 14));
			JTableHeader header = table.getTableHeader();
			table.getColumnModel().getColumn(0).setPreferredWidth(15);
			table.getColumnModel().getColumn(1).setPreferredWidth(15);
			table.getColumnModel().getColumn(2).setPreferredWidth(15);
			table.getColumnModel().getColumn(3).setPreferredWidth(15);
			
			DefaultTableCellRenderer renderer=new DefaultTableCellRenderer();
	        renderer.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
	        table.setDefaultRenderer(Object.class, renderer);
			
			header.setPreferredSize(new Dimension(header.getWidth(), 35));
			table.setPreferredScrollableViewportSize(new Dimension(410,380));
			table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		    JScrollPane scroll = new JScrollPane(table);
		    scroll.setSize(180,100);
		    tJPanel.add(scroll);
		    
		    JButton okbnt = new JButton("确定");
		    okbnt.setLocation(400, 500);
		    okbnt.setSize(10, 10);
		    okbnt.setFont(new Font("华文宋体", Font.PLAIN,15));
		    okbnt.setForeground(Color.blue);
		    okbnt.setBackground(Color.white);
		    bJPanel.add(okbnt);
		   
		    okbnt.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
				for (int i = 0; i < table.getModel().getColumnCount() - 1; i++) {
		
					idList.add((String) table.getModel().getValueAt(i, 1));
					groupList.add((String) table.getModel().getValueAt(i, 0));
					radioAreaList.add((String) table.getModel().getValueAt(i, 2));
				}
//				System.out.println(idList.toString());
//				System.out.println(groupList.toString());
//				System.out.println(radioAreaList.toString());
				int group = Integer.parseInt(textField_1.getText());
				int[] arrayArea = new int[group];
				
				for(int j=0;j<idList.size();j++){
					for(int i=0;i<group;i++){
						if(groupList.get(j+1) == groupList.get(j)){
							arrayArea[i] += Integer.parseInt(radioAreaList.get(i));
						}
						else {
							arrayArea[i+1] += Integer.parseInt(radioAreaList.get(i));
						}
					}
				}
				for(int i=0;i<idList.size();i++){
					rowData[i][3] = String.valueOf(arrayArea[i]);
				}
			}
			  });
		    
		    frame1.getContentPane().add(tJPanel,BorderLayout.CENTER);
		    frame1.getContentPane().add(bJPanel, BorderLayout.SOUTH);
		    frame1.setVisible(true);
		    frame1.setSize(610,610);
		    frame1.setLocationRelativeTo(null);
		    frame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);	
		}

	
}
